/*
 * @Author: doing
 * @Date: 2023-08-10 09:36:21
 * @LastEditors: git config user.name && git config user.email
 * @LastEditTime: 2023-08-23 17:03:40
 * @FilePath: \admin\src\composables\auth.js
 * @Description: 
 * 从cookie中获取token和设置token
 * Copyright (c) 2023 by 1540265624@qq.com, All Rights Reserved. 
 */
import { useCookies } from '@vueuse/integrations/useCookies'

const topUrl = `${import.meta.env.VITE_AUTH_DOMAIN}`;
const cookie = useCookies()
const TokenKey = {
    accessToken: '',
    refreshToken: '',
    userInfo: ''
};

//获取token
export function getToken(){
    TokenKey.accessToken = cookie.get('accessToken')
    TokenKey.refreshToken = cookie.get('refreshToken')
    TokenKey.userInfo = cookie.get('userInfo')
    return TokenKey
}

//设置token
export function setToken(token){
    // return cookie.set(TokenKey,token)
    //保存cookie的顶级域名
    const expiresInMilliseconds = 86400000; // 24 小时的毫秒数
    const expirationDate = new Date(Date.now() + expiresInMilliseconds);
    return cookie.set(TokenKey, token, { domain: topUrl, expirationDate });
}

//清除token
export function removeToken(){
    cookie.remove('accessToken')
    cookie.remove('refreshToken')
    cookie.remove('userInfo')
}


